package com.example.teamtribe.common.model;

import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.json.JsonTeam;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by lenovo on 2018/5/26.
 */

public class TeamModel {
    private static TeamModel instance;
    private TeamModel() {}
    public static TeamModel getInstance() {
        if (instance == null) {
            synchronized (TeamModel.class) {
                if (instance == null) {
                    instance = new TeamModel();
                }
            }
        }
        return instance;
    }

    public void getHead(final String fileName, final FileModel.FileBack back) {
        FileModel.getInstance().download(fileName, 0, back);
    }

    public void getTeamNoHead(int id, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        HttpUtil.post(URLStore.TEAM_INFO_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                JsonTeam jsonTeam = new Gson().fromJson(json, JsonTeam.class);
                Log.d("team", json);

                final int status = jsonTeam.getCode();
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("team", jsonTeam.getTeam());
                    back.success(bundle);
                }
            }
        });
    }

    public void getTeam(int id, final FileModel.FileBack back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        HttpUtil.post(URLStore.TEAM_INFO_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                JsonTeam jsonTeam = new Gson().fromJson(json, JsonTeam.class);
                Log.d("team", json);

                final int status = jsonTeam.getCode();
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    final Team team = jsonTeam.getTeam();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("team", team);
                    back.success(bundle);

                    getHead(team.getPortrait(), back);
                }
            }
        });
    }

}

